package com.syt.cellphone.information.ui.widget

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet
import android.view.Menu
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.StyleRes
import androidx.appcompat.widget.Toolbar
import com.google.android.material.appbar.AppBarLayout
import com.syt.cellphone.information.R
import com.syt.cellphone.information.help.AppConfig
import com.syt.cellphone.information.lib.theme.elevation
import com.syt.cellphone.information.lib.theme.primaryColor
import com.syt.cellphone.information.utils.activity
import com.syt.cellphone.information.utils.navigationBarHeight
import com.syt.cellphone.information.utils.statusBarHeight
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.bottomPadding
import org.jetbrains.anko.topPadding

/**
 *  @author: syt
 *  创建时间: 2020/8/25 14:13
 *  @功能   自定义标题栏
 *  @version v1.0
 */
class TitleBar(context: Context, attributeSet: AttributeSet?) : AppBarLayout(context, attributeSet) {

    val toolbar: Toolbar
    // 菜单
    val menu: Menu
        get() = toolbar.menu
    // 标题
    var title: CharSequence?
        get() = toolbar.title
        set(title) {
            toolbar.title = title
        }
    // 字幕
    var subtitle: CharSequence?
        get() = toolbar.subtitle
    set(subtitle) {
        toolbar.subtitle = subtitle
    }

    private val displayHomeAsUp: Boolean
    private val navigationIconTint: ColorStateList?
    private val navigationIconTintMode: Int
    private val attachToActivity: Boolean

    init {
        View.inflate(context, R.layout.view_title_bar, this)
        toolbar = findViewById(R.id.toolbar)
        val a = context.obtainStyledAttributes(
            attributeSet,
            R.styleable.TitleBar,
            R.attr.titleBarStyle,
            0
        )
        navigationIconTint = a.getColorStateList(R.styleable.TitleBar_navigationIconTint)
        navigationIconTintMode = a.getInt(R.styleable.TitleBar_navigationIconTintMode, 9)
        attachToActivity = a.getBoolean(R.styleable.TitleBar_attachToActivity, true)
        displayHomeAsUp = a.getBoolean(R.styleable.TitleBar_displayHomeAsUp, true)

        val navigationIcon = a.getDrawable(R.styleable.TitleBar_navigationIcon)
        val navigationContentDescription =
            a.getText(R.styleable.TitleBar_navigationContentDescription)
        val titleText = a.getString(R.styleable.TitleBar_title)
        val subtitleText = a.getString(R.styleable.TitleBar_subtitle)

        toolbar.apply {
            // 图标 导航内容说明
            navigationIcon?.let {
                this.navigationIcon = it
                this.navigationContentDescription = navigationContentDescription
            }
            // 设置标题文字外观
            if (a.hasValue(R.styleable.TitleBar_titleTextAppearance)) {
                this.setTitleTextAppearance(context, a.getResourceId(R.styleable.TitleBar_titleTextAppearance, 0))
            }
            // 标题栏标题文字颜色
            if (a.hasValue(R.styleable.TitleBar_titleTextColor)) {
                this.setTitleTextColor(a.getColor(R.styleable.TitleBar_titleTextColor, -0x1))
            }
            // 标题栏字幕文字外观
            if (a.hasValue(R.styleable.TitleBar_subtitleTextAppearance)) {
                this.setSubtitleTextAppearance(context, a.getResourceId(R.styleable.TitleBar_subtitleTextAppearance, 0))
            }
            // 标题栏字幕文字颜色
            if (a.hasValue(R.styleable.TitleBar_subtitleTextColor)) {
                this.setSubtitleTextColor(a.getColor(R.styleable.TitleBar_subtitleTextColor, -0x1))
            }
            // 标题栏内容向左插入 || 标题栏内容右插入
            if (a.hasValue(R.styleable.TitleBar_contentInsetLeft)
                || a.hasValue(R.styleable.TitleBar_contentInsetRight)) {
                this.setContentInsetsAbsolute(
                    a.getDimensionPixelSize(R.styleable.TitleBar_contentInsetLeft, 0),
                    a.getDimensionPixelSize(R.styleable.TitleBar_contentInsetRight, 0)
                )
            }
            // 标题栏内容插入开始 || 标题栏内容插入结束
            if (a.hasValue(R.styleable.TitleBar_contentInsetStart)
                || a.hasValue(R.styleable.TitleBar_contentInsetEnd)
            ) {
                this.setContentInsetsRelative(
                    a.getDimensionPixelSize(R.styleable.TitleBar_contentInsetStart, 0),
                    a.getDimensionPixelSize(R.styleable.TitleBar_contentInsetEnd, 0)
                )
            }
            // 标题栏内容插图从导航开始
            if (a.hasValue(R.styleable.TitleBar_contentInsetStartWithNavigation)) {
                this.contentInsetStartWithNavigation = a.getDimensionPixelOffset(
                    R.styleable.TitleBar_contentInsetStartWithNavigation, 0
                )
            }
            // 标题栏内容插入动作结束
            if (a.hasValue(R.styleable.TitleBar_contentInsetEndWithActions)) {
                this.contentInsetEndWithActions = a.getDimensionPixelOffset(
                    R.styleable.TitleBar_contentInsetEndWithActions, 0
                )
            }
            // 插入标题
            if (!titleText.isNullOrBlank()) {
                this.title = titleText
            }
            // 插入内容
            if (!subtitleText.isNullOrBlank()) {
                this.subtitle = subtitleText
            }
            // 标题栏内容布局
            if (a.hasValue(R.styleable.TitleBar_contentLayout)) {
                inflate(context, a.getResourceId(R.styleable.TitleBar_contentLayout, 0), this)
            }
        }
        // 状态栏高度
        if (a.getBoolean(R.styleable.TitleBar_fitStatusBar, true)) {
            topPadding = context.statusBarHeight
        }
        // 导航栏高度
        if (a.getBoolean(R.styleable.TitleBar_fitNavigationBar, false)) {
            bottomPadding = context.navigationBarHeight
        }

        backgroundColor = context.primaryColor

        stateListAnimator = null
        elevation = if (AppConfig.elevation < 0) {
            context.elevation
        } else {
            AppConfig.elevation.toFloat()
        }
        // 回收
        a.recycle()
    }

    fun setNavigationOnClickListener(clickListener: ((View) -> Unit)) {
        toolbar.setNavigationOnClickListener(clickListener)
    }

    fun setTitle(titleId: Int) {
        toolbar.setTitle(titleId)
    }

    fun setSubTitle(subtitleId: Int) {
        toolbar.setSubtitle(subtitleId)
    }

    fun setTitleTextColor(@ColorInt color: Int) {
        toolbar.setTitleTextColor(color)
    }

    fun setTitleTextAppearance(@StyleRes resId: Int) {
        toolbar.setTitleTextAppearance(context, resId)
    }

    fun setSubTitleTextColor(@ColorInt color: Int) {
        toolbar.setSubtitleTextColor(color)
    }

    fun setSubTitleTextAppearance(@StyleRes resId: Int) {
        toolbar.setSubtitleTextAppearance(context, resId)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        attachToActivity()
    }

    // 透明
    fun transparent() {
        elevation = 0f
        backgroundColor = Color.TRANSPARENT
    }

    // 将标题设置到活动上去
    private fun attachToActivity() {
        if (attachToActivity) {
            activity?.let {
                it.setSupportActionBar(toolbar)
                it.supportActionBar?.setDisplayHomeAsUpEnabled(displayHomeAsUp)
            }
        }
    }
}